package org.gdkm.springbootxiangmushizhan.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.gdkm.springbootxiangmushizhan.domain.vo.UserAdminVO;
import org.gdkm.springbootxiangmushizhan.domain.po.User;
import java.util.List;

@Mapper
public interface UserMapper {
    // 分页查询所有用户
    @Select("SELECT id, username, email, created, valid FROM t_user ORDER BY id DESC")
    List<UserAdminVO> selectAllUsersWithPage();

    // 根据id删除用户
    @Delete("DELETE FROM t_user WHERE id = #{id}")
    void deleteUserById(Integer id);

    // 根据id修改valid状态
    @Update("UPDATE t_user SET valid = #{valid} WHERE id = #{id}")
    void updateUserValidById(@Param("id") Integer id, @Param("valid") Integer valid);

    // 根据id查询用户信息
    @Select("SELECT id, username, password, email, created, modified, valid FROM t_user WHERE id = #{id}")
    User selectUserById(Integer id);

    // 根据id更新用户名、邮箱、密码、修改时间
    @Update("UPDATE t_user SET username = #{username}, email = #{email}, password = #{password}, modified = #{modified} WHERE id = #{id}")
    void updateUserById(User user);

    // 新增用户
    @org.apache.ibatis.annotations.Insert("INSERT INTO t_user (username, password, email, created, modified, valid) VALUES (#{username}, #{password}, #{email}, #{created}, #{modified}, #{valid})")
    @org.apache.ibatis.annotations.Options(useGeneratedKeys = true, keyProperty = "id")
    void insertUser(User user);

    // 新增：为用户分配权限
    @org.apache.ibatis.annotations.Insert("INSERT INTO t_user_authority (user_id, authority_id) VALUES (#{userId}, #{authorityId})")
    void insertUserAuthority(@Param("userId") Integer userId, @Param("authorityId") Integer authorityId);
} 